/**
 * Project Name:九色鹿
 * File Name:AutoNumService.java
 * Package Name:com.fxp.project.express.service.impl
 * Date:2021年09月14日 13:47
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.express.service.impl;

import com.fxp.project.express.entity.request.BaseRequest;
import com.fxp.project.express.entity.response.AutoNumResp;
import com.fxp.project.express.provider.AutoNumProvider;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * ClassName:AutoNumService<br/>
 * Function: 自动单号的业务对象<br/>
 * Date:2021年09月14日 13:47<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK 8
 */
@Data
@Log4j2
@Component
public class AutoNumService {

    private final AutoNumProvider provider;

    private volatile static AutoNumService instance;

    /**
     * initInstance: 获取单例的实体对象<br/>
     *
     * @param provider
     * @author Jason.Wang
     * @date 2021/9/13 17:45
     */
    public static void initInstance(AutoNumProvider provider) {
        if (instance == null) {
            synchronized (AutoNumService.class) {
                if (instance == null) {
                    instance = new AutoNumService(provider);
                }
            }
        }
    }

    /**
     * getInstance: 获取单例对象方法<br/>
     *
     * @return
     * @author Jason.Wang
     * @date 2021/9/13 17:46
     */
    public static AutoNumService getInstance() {
        return instance;
    }

    /**
     * AutoNumService: 私有构建方法<br/>
     *
     * @param provider
     * @author Jason.Wang
     * @date 2021/9/13 17:46
     */
    private AutoNumService(AutoNumProvider provider) {
        this.provider = provider;
    }

    /**
     * getComByNumList: 根据快递单号返回快递公司<br/>
     *
     * @param request
     * @return
     * @throws Exception
     * @author Jason.Wang
     * @date 2021/9/14 14:36
     */
    public List<AutoNumResp> getComByNumList(BaseRequest request) throws Exception {
        return provider.getComByNumList(request);
    }

    /**
     * getFirstComByNum: 根据单号获取第一个快递公司<br/>
     *
     * @param request
     * @return
     * @throws Exception
     * @author Jason.Wang
     * @date 2021/9/14 14:39
     */
    public AutoNumResp getFirstComByNum(BaseRequest request) throws Exception {
        List<AutoNumResp> list = this.getComByNumList(request);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

}